package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.dto.CompleteInfo;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoBigHomeWorkTask;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoSheHuiShiJianTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 做大作业
 */
public class DoSheHuiShiJian {


    public static void main(String[] args) throws Exception {

//        // D:\project\answer-tool\libs\lmsAnswerInfo\阿迪力\形势与政策
//        String completeInfoFilePath2 = "D:\\project\\answer-tool\\libs\\lmsAnswerInfo\\阿迪力\\形势与政策\\2065001255839-practice.txt";
//        File completeInfoFile2 = new File(completeInfoFilePath2);
//        boolean isComplete2 = false;
//        System.err.println("completeInfoFile2.exists()"+completeInfoFile2.exists());
//        if(completeInfoFile2.exists()){
//            List<CompleteInfo> completeInfoList = JSON.parseArray(FileUtils.readFileToString(completeInfoFile2,"UTF-8"),
//                    CompleteInfo.class);
//            for(CompleteInfo completeInfo:completeInfoList){
//                if(completeInfo.isComplete()){
//                    isComplete2 = true;
//                    break;
//                }
//            }
//        }
//        System.err.println("isComplete:"+isComplete2);
//        Thread.sleep(1200*60*60*3);
        int sync = 8;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();

        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李俊杰");
        teachers.add("阿迪力");
        for(String teacher:teachers){
            InputStream inputStream2 = new FileInputStream("libs//2022秋//teacher//"+teacher+".user");
            List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[0];
                String pwd = strs[1];
                LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
                lmsWatchDTO.setUsername(userCode);
                lmsWatchDTO.setPassword(pwd);
                lmsWatchDTO.setTeacher(teacher);

                Map<Long, String> courseMap = LmsUtil.getUserCourseMap(userCode);
                if (null == courseMap) {
                    continue;
                }
                Map<Long, String> newCourseMap = Maps.newHashMap();

                for (Long cId : courseMap.keySet()) {
                    String subjectName = courseMap.get(cId);
                    if (Strings.isNullOrEmpty(subjectName)) {
                        continue;
                    }
                    if (
                            subjectName.equals("形势与政策")
                        // || subjectName.equals("习近平新时代中国特色社会主义思想")
                        //    ||subjectName.equals("中国近现代史纲要")
                        //     || subjectName.equals("习近平总书记教育重要论述研究")
                        //   || subjectName.equals("毛泽东思想和中国特色社会主义理论体系概论")
                        //      || subjectName.equals("思想道德与法治")
                    ) {

                        String completeInfoFilePath = "libs//lmsAnswerInfo//" +teacher+"//"+ subjectName + "//" + userCode + "-practice.txt";
                        File completeInfoFile = new File(completeInfoFilePath);
                        boolean isComplete = false;
                        if(completeInfoFile.exists()){
                            List<CompleteInfo> completeInfoList = JSON.parseArray(FileUtils.readFileToString(completeInfoFile,"UTF-8"),
                                    CompleteInfo.class);
                            for(CompleteInfo completeInfo:completeInfoList){
                                if(completeInfo.isComplete()){
                                    isComplete = true;
                                    break;
                                }
                            }
                        }
                        if(!isComplete){
                            newCourseMap.put(cId, subjectName);
                        }
                    }
                }

                if(newCourseMap.size()<1){
                    continue;
                }

                lmsWatchDTO.setCourseMap(newCourseMap);


                watchQueue.add(lmsWatchDTO);

            }
        }



        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new DoSheHuiShiJianTask("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }
    }


}
